// 
 #include "stdafx.h"
#include "Common.h"
#include "LogManager.h"
/*#include "NGLog.h"*/
#include <stdarg.h>
#include "Exception.h"
#include "TextLogger.h"
#define DEF_VALUE_NOT_SET 0xDEADBEEF


namespace Apo
{
	//static logManager *serverRoot::gLogManger = NULL;

	//time_t UNIXTIME;
#ifndef WIN32
	static const char* colorstrings[TBLUE+1] = {
		"",
		"\033[22;31m",
		"\033[22;32m",
		"\033[01;33m",
		//"\033[22;37m",
		"\033[0m",
		"\033[01;37m",
		"\033[22;34m",
	};
#endif
 
	logManager gLogManger;
	logManager::logManager()
	{



		int file_log_level = DEF_VALUE_NOT_SET;
		int screen_log_level = DEF_VALUE_NOT_SET;
		int do_version = 0;
		if(!do_version )
		{
			launch_thread(new TextLoggerThread);
			this->Init(1, 3);
		}
		else
		{
			this->m_fileLogLevel = -1;
			this->m_screenLogLevel = 3;
		}

	}
	logManager::~logManager()
	{
		 
	}


// 	logManager * getInstance()
// 	{
// 		//return &gglogManager;
// 	}
	void logManager::outString( const char * str, ... )
	{
		if(m_fileLogLevel < 0 && m_screenLogLevel < 0)
			return;

		va_list ap;
		va_start(ap, str);

		if(m_fileLogLevel >= 0)
			fileLogger->AddLineSFormat(true, str, ap);

		if(m_screenLogLevel >= 0)
		{
			vprintf(str, ap);
			putc('\n', stdout);
		}

		va_end(ap);
	}

	void logManager::outError( const char * err, ... )
	{
		if(m_fileLogLevel < 1 && m_screenLogLevel < 1)
			return;

		va_list ap;
		va_start(ap, err);

		if(m_fileLogLevel >= 1)
			fileLogger->AddLineSFormat(true, err, ap);

		if(m_screenLogLevel >= 1)
		{
#ifdef WIN32
			SetConsoleTextAttribute(stderr_handle, FOREGROUND_RED | FOREGROUND_INTENSITY);
#else
			puts(colorstrings[TRED]);
#endif
			vfprintf(stderr, err, ap);
			putc('\n', stderr);
#ifdef WIN32
			SetConsoleTextAttribute(stderr_handle, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN);
#else
			puts(colorstrings[TNORMAL]);
#endif
		}

		va_end(ap);
	}

	void logManager::outBasic( const char * str, ... )
	{
		if(m_fileLogLevel < 1 && m_screenLogLevel < 1)
			return;

		va_list ap;
		time_t t = time(NULL);
		va_start(ap, str);

		if(m_fileLogLevel >= 1)
			fileLogger->AddLineSFormat(true, str, ap);

		if(m_screenLogLevel >= 1)
		{
			vprintf(str, ap);
			putc('\n', stdout);
		}

		va_end(ap);
	}

    //void outBasic( const char * str1, const char * str2, ... );


	void logManager::outDetail( const char * str, ... )
	{
		if(m_fileLogLevel < 2 && m_screenLogLevel < 2)
			return;

		va_list ap;
		va_start(ap, str);

		if(m_fileLogLevel >= 2)
			fileLogger->AddLineSFormat(true, str, ap);

		if(m_screenLogLevel >= 2)
		{
			vprintf(str, ap);
			putc('\n', stdout);
		}

		va_end(ap);
	}

	void logManager::outDebug( const char * str, ... )
	{
		if(m_fileLogLevel < 3 && m_screenLogLevel < 3)
			return;

		va_list ap;
		va_start(ap, str);

		if(m_fileLogLevel >= 3)
			fileLogger->AddLineSFormat(true, str, ap);

		if(m_screenLogLevel >= 3)
		{
			vprintf(str, ap);
			putc('\n', stdout);
		}

		va_end(ap);
	}

	void logManager::outMenu( const char * str, ... )
	{
		va_list ap;
		va_start(ap, str);
		vprintf( str, ap );
		va_end(ap);
		fflush(stdout);
	}

	void logManager::Init(int fileLogLevel, int screenLogLevel)
	{
		m_screenLogLevel = screenLogLevel;
		m_fileLogLevel = fileLogLevel;
		fileLogger = new TextLogger(FormatOutputString("logs", "ServerLog", true).c_str(), true);

		if(m_fileLogLevel >= 0)
			fileLogger->Open();

		// get error handle
#ifdef WIN32
		stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
		stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
#endif
	}

	void logManager::SetScreenLoggingLevel(int level)
	{
		m_screenLogLevel = level;
	}

	void logManager::SetFileLoggingLevel(int level)
	{
		if(level < 0)
		{
			if(fileLogger->IsOpen())
				fileLogger->Close();
		}
		else
		{
			if(!fileLogger->IsOpen())
				fileLogger->Open();
		}
		m_fileLogLevel = level;
	}

	void logManager::outColor(unsigned int colorcode, const char * str, ...)
	{
		if( !str ) return;
		va_list ap;
		va_start(ap, str);
#ifdef WIN32
		SetConsoleTextAttribute(stdout_handle, colorcode);
#else
		printf(colorstrings[colorcode]);
#endif
		vprintf( str, ap );
		fflush(stdout);
		va_end(ap);
	}
   


 

}



 
